<?php

use Illuminate\Database\Migrations\Migration;

class CreateBaseTables extends Migration {

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		// Create products table
		Schema::create("pages", function($table)
		{
			// identifiers
			$table->increments("id");
			$table->integer("parentid");
			
			// layout
			$table->integer("pagetemplateid");
			$table->boolean("is_homepage");
			$table->integer("pageorder");
			
			// metadata
			$table->string("name");
			$table->string("slug");

			// SEO
			$table->string("title")->nullable();
			$table->text("description")->nullable();
			$table->text("keywords")->nullable();
			
			// content
			$table->text("content")->nullable();

			// soft deletes
			$table->softDeletes();
			
			// timestamps
			$table->timestamps();
		});

		// Create page template table
		Schema::create("page_templates", function($table)
		{
			// indentifiers
			$table->increments("id");

			// decriptors
			$table->string("name");
			$table->string("title");

			// timestamps
			$table->timestamps();
		});

		// Create products table
		Schema::create("products", function($table)
		{
			// indentifiers
			$table->increments("id");
			$table->integer("categoryid");

			// decriptors
			$table->string("name");
			$table->string("slug");
			$table->text("shortdescription");
			$table->text("description");

			// soft deletes
			$table->softDeletes();

			// timestamps
			$table->timestamps();
		});

		// Create product category table
		Schema::create("product_categories", function($table)
		{
			// indentifiers
			$table->increments("id");
			$table->integer("parentid");

			// decriptors
			$table->string("name");
			$table->string("slug");
			$table->string("title");

			// soft deletes
			$table->softDeletes();

			// timestamps
			$table->timestamps();
		});

		// Create product category map table
		Schema::create("products_product_categories", function($table)
		{
			// indentifiers
			$table->integer("productid");
			$table->integer("categoryid");

			// timestamps
			$table->timestamps();
		});

		// Create news table
		Schema::create("news", function($table)
		{
			// indentifiers
			$table->increments("id");

			// decriptors
			$table->date("date");
			$table->string("title");
			$table->string("slug");
			$table->text("body");
			$table->string("feature_image");

			// soft deletes
			$table->softDeletes();

			// timestamps
			$table->timestamps();
		});

		/*Schema::create('users', function($table)
		{
			$table->increments('id');
			$table->string('email');
			$table->string('password');
			$table->text('permissions')->nullable();
			$table->boolean('activated')->default(0);
			$table->string('activation_code')->nullable();
			$table->timestamp('activated_at')->nullable();
			$table->timestamp('last_login')->nullable();
			$table->string('persist_code')->nullable();
			$table->string('reset_password_code')->nullable();
			$table->string('first_name')->nullable();
			$table->string('last_name')->nullable();
			$table->timestamps();

			// We'll need to ensure that MySQL uses the InnoDB engine to
			// support the indexes, other engines aren't affected.
			$table->engine = 'InnoDB';
			$table->unique('email');
			$table->index('activation_code');
			$table->index('reset_password_code');
		});

		Schema::create('groups', function($table)
		{
			$table->increments('id');
			$table->string('name');
			$table->text('permissions')->nullable();
			$table->timestamps();

			// We'll need to ensure that MySQL uses the InnoDB engine to
			// support the indexes, other engines aren't affected.
			$table->engine = 'InnoDB';
			$table->unique('name');
		});

		Schema::create('users_groups', function($table)
		{
			$table->integer('user_id')->unsigned();
			$table->integer('group_id')->unsigned();

			// We'll need to ensure that MySQL uses the InnoDB engine to
			// support the indexes, other engines aren't affected.
			$table->engine = 'InnoDB';
			$table->primary(array('user_id', 'group_id'));
		});

		Schema::create('throttle', function($table)
		{
			$table->increments('id');
			$table->integer('user_id')->unsigned();
			$table->string('ip_address')->nullable();
			$table->integer('attempts')->default(0);
			$table->boolean('suspended')->default(0);
			$table->boolean('banned')->default(0);
			$table->timestamp('last_attempt_at')->nullable();
			$table->timestamp('suspended_at')->nullable();
			$table->timestamp('banned_at')->nullable();

			// We'll need to ensure that MySQL uses the InnoDB engine to
			// support the indexes, other engines aren't affected.
			$table->engine = 'InnoDB';
		});*/

		/*
		DB::statement("ALTER TABLE pages CHANGE `updated_at` `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP");
		DB::statement("ALTER TABLE page_templates CHANGE `updated_at` `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP");
		DB::statement("ALTER TABLE product_categories CHANGE `updated_at` `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP");
		DB::statement("ALTER TABLE products CHANGE `updated_at` `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP");
		DB::statement("ALTER TABLE product_category_map CHANGE `updated_at` `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP");
		DB::statement("ALTER TABLE news CHANGE `updated_at` `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP");
		*/
	}

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		// Drop tables
		Schema::drop("pages");
		Schema::drop("page_templates");
		Schema::drop("product_categories");
		Schema::drop("products");
		Schema::drop("products_product_categories");
		Schema::drop("news");
		/*Schema::drop("users");
		Schema::drop("groups");
		Schema::drop("users_groups");
		Schema::drop("throttle");*/
		
	}

}